﻿using System.Windows.Controls;
using System.Windows.Media;
using TicTacToe.Core;

namespace TicTacToe.UI.Components
{
    internal partial class PlayerPropertyControl : UserControl
    {
        public delegate void BoolDlg(bool valid);
        public event BoolDlg PlayerPropsChanged;

        private bool IsPlayerChosen { get; set; }

        public Player Player { get { return new Player(nameTBox.Text, colorPicker.SelectedColor); } }
        public string PlayerName { get { return nameTBox.Text; } }

        public Color SelectedColor
        {
            set
            {
                colorPicker.SelectedColor = value;
            }
        }

        public PlayerPropertyControl()
        {
            InitializeComponent();

            nameTBox.TextChanged += ValidateName;
            colorPicker.SelectedColor = colorPicker.AvailableColors.Random().Color;
        }

        void ValidateName(object sender, TextChangedEventArgs e)
        {
            if (Player.IsValidName(nameTBox.Text))
            {
                IsPlayerChosen = true;
            }
            else
            {
                IsPlayerChosen = false;
            }

            if (PlayerPropsChanged != null) PlayerPropsChanged(IsPlayerChosen);
        }
    }
}
